package com.cloudkinto.service.covershows.impl;

import com.cloudkinto.common.utils.ali.AliOss;
import com.cloudkinto.entity.CustomerCaseDo;
import com.cloudkinto.dao.CustomerCaseDao;
import com.cloudkinto.entity.NewsInformationDo;
import com.cloudkinto.service.covershows.CustomerCaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import com.cloudkinto.service.covershows.vo.CustomerCaseAddReq;
import com.cloudkinto.service.covershows.vo.CustomerCaseDetailRes;
import com.cloudkinto.service.covershows.vo.CustomerCasePageRes;
import com.cloudkinto.service.covershows.vo.CustomerCaseUpdateReq;
import com.cloudkinto.utils.FreeMarkerUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-10-12
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class CustomerCaseServiceImpl extends ServiceImpl<CustomerCaseDao, CustomerCaseDo> implements CustomerCaseService {
    @Autowired
    private CustomerCaseDao dao;


    @Override
    public List<CustomerCaseDetailRes> detail() {
        List<CustomerCaseDetailRes> list = new ArrayList();
        List<CustomerCaseDo> customerCaseDos = dao.selectList(null);
        for (CustomerCaseDo customerCaseDo : customerCaseDos) {
            customerCaseDo.setBrowseCount(customerCaseDo.getBrowseCount() + 1);
            customerCaseDo.setCollectCount(customerCaseDo.getCollectCount() + 1);
            dao.updateById(customerCaseDo);
            CustomerCaseDetailRes res = new CustomerCaseDetailRes();
            BeanUtils.copyProperties(customerCaseDo, res);
            if (StringUtils.isNotBlank(customerCaseDo.getAntistop())) {
                String[] split = customerCaseDo.getAntistop().split(";");
                res.setAntistopList(Arrays.asList(split));
            }
            res.setBrowseCount("1w+");
            res.setCollectCount("1w+");
            list.add(res);
        }
        return list;
    }


    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<CustomerCaseDo> wrapper = queryBuild(map);
        IPage<CustomerCaseDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<CustomerCasePageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }


    //查询分页列表之后对列表数据的处理
    private List<CustomerCasePageRes> handlerListAfter(List<CustomerCaseDo> list) {
        List<CustomerCasePageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(newsInformationDo -> {
                CustomerCasePageRes res = new CustomerCasePageRes();
                BeanUtils.copyProperties(newsInformationDo, res);
                res.setBrowseCount("1w+");
                res.setCollectCount("1w+");
                if (StringUtils.isNotBlank(newsInformationDo.getAntistop())) {
                    String[] split = newsInformationDo.getAntistop().split(";");
                    res.setAntistopList(Arrays.asList(split));
                }
                result.add(res);
            });
        }
        return result;
    }

    private IPage<CustomerCaseDo> pageInit(Map<String, String> map) {
        IPage<CustomerCaseDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 6);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<CustomerCaseDo> queryBuild(Map<String, String> map) {
        QueryWrapper<CustomerCaseDo> wrapper = new QueryWrapper<>();
//        if (StringUtils.isNotBlank(map.get("keyword"))) {
//            wrapper.like("keyword", map.get("keyword"));
//        }
        return wrapper.orderByDesc("id");
    }


}
